﻿using Gonte.Wpf.Controls.ViewModel;
using Gonte.Wpf.Controls.ViewModel.Searching;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyWpfControlsTest
{
    [TestClass()]
    public class ViewModelSearcherTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public class ParentObject
        {
            private string _parentName;

            public string ParentName
            {
                get { return _parentName; }
                set { _parentName = value; }
            }
        }

        public class ItemObject
        {
            private string _itemName;

            public string ItemName
            {
                get { return _itemName; }
                set { _itemName = value; }
            }
        }

        [TestMethod()]
        public void ViewModelSearcherSearchTest()
        {
            // Create the parent content
            ContentViewModelInfo contentViewModelInfo = new ContentViewModelInfo();

            ParentObject parentObject = new ParentObject();
            parentObject.ParentName = "MyTestParentName";

            ContentViewModel contentViewModel = new ContentViewModel(contentViewModelInfo, null, parentObject);

            ItemViewModelInfo itemViewModelInfo = new ItemViewModelInfo();

            ItemObject itemObject = new ItemObject();
            itemObject.ItemName = "MyTestItemName";

            ItemViewModel itemViewModel = new ItemViewModel(itemViewModelInfo, contentViewModel, itemObject);

            contentViewModel.Children.Add(itemViewModel);

            ViewModelSearcher searcher = new ViewModelSearcher(contentViewModel);

            searcher.IsMatch = delegate(string text, ItemViewModel viewModel)
            {
                object model = viewModel.Model;

                ParentObject parent = model as ParentObject;

                if (null != parent)
                {
                    return parent.ParentName.Contains(text);
                }

                ItemObject item = model as ItemObject;

                if (null != item)
                {
                    return item.ItemName.Contains(text);
                }

                return false;
            };

            searcher.SearchText = "Test";

            Assert.IsFalse(contentViewModel.IsSelected);
            Assert.IsFalse(contentViewModel.IsExpanded);

            bool found = searcher.Search();
            Assert.IsTrue(found);
            Assert.IsTrue(contentViewModel.IsSelected);
            Assert.IsFalse(contentViewModel.IsExpanded);

            found = searcher.Search();
            Assert.IsTrue(found); // Found
            Assert.IsTrue(itemViewModel.IsSelected);    // The child was selected
            Assert.IsTrue(contentViewModel.IsExpanded); // The parent was expanded
            Assert.IsFalse(contentViewModel.IsSelected); // Single selection

            searcher.SearchText = "NotFound";
            found = searcher.Search();
            Assert.IsFalse(found); // Found
            Assert.IsFalse(itemViewModel.IsSelected); 
        }
    }
}
